﻿using System;
using System.Collections.Generic;

using System.Collections;
using System.Linq;
using System.Text;
using System.Data;
using XMK.Framework.DataAccess;
using XMK.Tadong.Model;
using XMK.Utils;
using XMK.Framework.Model;
using XMK.Tadong.Model.Cond;
using System.Diagnostics;
using System.IO;
using System.Web;
using System.Threading;

namespace XMK.Tadong.StructuredData_IntoDb.Data
{
    public class DBTree
    {
         /// <summary>
        /// 数据库连接
        /// </summary>
        protected DbProvider dbProvider;

        public DBTree(DbProvider dbProvider)
        {
            this.dbProvider = dbProvider;

        }
        public DataTable GetInputTree()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from dict_table where isinput=1 order by xh");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;

        }
        public DataTable GetList_TableCode(string tablecode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from dict_col where table_code ='"+tablecode+"' and field_show =1" );
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;

        }
        public DataTable GetInputFields(string BDM)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from dict_col WHERE TABLE_ID =" +
                "(SELECT TABLE_ID FROM dict_table WHERE table_code='" + BDM.Trim().ToString() + "') order by field_id asc");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;

        }
        public  DataTable GetInputTableOraginData_Filter(string BDM, Hashtable p_Filter)
        {
            //获取字段项列表
            string FieldList = GetInputFieldsList(BDM);

            //如果数据为空
            if (FieldList == "")
                return null;
            else
            {
      
                string strWhere = "";
                if ((p_Filter != null) && (p_Filter.Count > 0))
                {

                    foreach (object key in p_Filter.Keys)
                    {
                        strWhere += key.ToString() + "='" + p_Filter[key.ToString()].ToString() + "' AND ";
                    }
                    if (strWhere.Length > 0)
                    {
                        strWhere = " where " + strWhere.Substring(0, strWhere.Length - 4);
                    }

                }
                string StrSql = "select  " + FieldList + "  from " + BDM + strWhere;
          

                DbCmdHelper cmd = new DbCmdHelper(dbProvider, StrSql.ToString());

                cmd.DbCmdInfo.CommandType = CommandType.Text;
                DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
                if (ds != null)
                {
                    return ds.Copy();
                }
                return null;

            }
        }
        /// <summary>
        /// 获取字段项列表
        /// </summary>
        /// <param name="BDM"></param>
        /// <returns></returns>
        public  string GetInputFieldsList(string BDM)
        {
            string FieldList = "";
            //获取字段项列表
            DataTable FieldData = GetInputFields(BDM);
            //如果数据为空
            if (FieldData != null && FieldData.Rows.Count != 0)
            {
                for (int k = 0; k < FieldData.Rows.Count; k++)
                {
                    if (FieldList == "")
                    {
                        //FieldList = FieldData.Rows[k]["fldname"].ToString();
                        FieldList = FieldData.Rows[k]["FIELD_CODE"].ToString();
                    }
                    else
                    {
                        //FieldList = FieldList + "," + FieldData.Rows[k]["fldname"].ToString();
                        FieldList = FieldList + "," + FieldData.Rows[k]["FIELD_CODE"].ToString();
                    }
                }
            }
            return FieldList;
        }

    }
}
